const db=require('../utils/mysql')
const { v4: uuidv4 } = require('uuid');
module.exports={
    newsAdd:async (info)=>{
    let newsId=uuidv4(),newroomId=uuidv4()
    return await new Promise((resolve,reject)=>{
        //先获取房间id
        const sql=`select * from news_link where fromId='${info.fromId}' and toId='${info.toId}'`
        let roomId=''
        db(sql,(err,data)=>{
            if(err) reject(err);
            //查不到该用户
            if (data.length==0){
                //插入两条记录
                const sql1=`INSERT INTO news_link (fromId,toId,fromName,toName,roomId,lastnew) values ('${info.fromId}','${info.toId}','${info.fromName}','${info.toName}','${newroomId}','${info.msg}')`
                const sql2=`INSERT INTO news_link (fromId,toId,fromName,toName,roomId,lastnew) values ('${info.toId}','${info.fromId}','${info.toName}','${info.fromName}','${newroomId}','${info.msg}')`
                db(sql1,(err,data)=>{
                    if(err) reject(err);
                })
                db(sql2,(err,data)=>{
                    if(err) reject(err);
                })

                //插入聊天记录
                const sql=`INSERT INTO news (newsId,fromId,toId,toName,msg,time,roomId,fromName)
                values ('${newsId}','${info.fromId}','${info.toId}','${info.toName}','${info.msg}','${info.time}','${newroomId}','${info.fromName}')`
                db(sql,(err,data)=>{
                    if(err) reject(err);
                    resolve({
                        msg:'消息添加成功',
                        status:200,
                        data:{
                            newsId:newsId,
                            fromId:info.fromId,
                            toId:info.toId,
                            msg:info.msg,
                            toName:info.toName,
                            time:info.time,
                            roomId:newroomId,
                            fromName:info.fromName
                        }

                    })
                })


            }else {
                let userroomId=data[0].roomId
                const sql=`INSERT INTO news (newsId,fromId,toId, fromName,toName,msg,time,roomId)
                values ('${newsId}','${info.fromId}','${info.toId}','${info.fromName}','${info.toName}','${info.msg}','${info.time}','${userroomId}')`
                db(sql,(err,data)=>{
                    if(err) reject(err);
                    //更新最新一条消息的记录
                    const sql1=`update news_link set lastnew='${info.msg}' where roomId='${userroomId}'`
                    db(sql1,(err,data)=>{
                        if(err) reject(err);
                    })
                    resolve({
                        msg:'消息添加成功',
                        status:200,
                        data:{
                            newsId:newsId,
                            fromId:info.fromId,
                            toId:info.toId,
                            msg:info.msg,
                            toName:info.toName,
                            time:info.time,
                            roomId:userroomId,
                            fromName:info.fromName
                        }
                    })
                })
            }
        })
    })
},
    getNewsList:async (ctx,next)=>{
        let query=ctx.request.query
        let result= await new Promise((resolve,reject)=>{
            const sql1=`select * from news_link where fromId='${query.fromId}'`
            // let list=new Map()
            db(sql1,(err,data)=>{
                if(err) reject(err);
                console.log(data)
                resolve({
                    msg:'消息列表获取成功',
                    status:200,
                    data:data
                })
            })
        })
        ctx.body=result
    },
    getNewsInfo:async (ctx,next)=>{
        let query=ctx.request.query
        let result= await new Promise((resolve,reject)=>{
            let sql=''
            if(query.roomId){
                sql=`select * from news where roomId='${query.roomId}' order  by time asc`
                db(sql,(err,data)=>{
                    if(err) reject(err);
                    resolve({
                        msg:'消息详情获取成功',
                        status:200,
                        data:data
                    })
                })
            }else {
                const sql1=`select * from news_link where fromId='${query.fromId}' and toId='${query.toId}'`
                db(sql1,(err,data)=>{
                    if(err) reject(err);
                    console.log(data,9)
                    if (data.length>0){
                        const sql2=`select * from news where roomId='${data[0].roomId}'`
                        db(sql2,(err,data)=>{
                            if(err) reject(err);
                            console.log(data,2)
                            resolve({
                                msg:'消息详情获取成功',
                                status:200,
                                data:data
                            })
                        })
                    }

                })

            }

        })
        ctx.body=result

    },
    onlineChange:(userId,online)=>{
        const sql=`update news_link set online=${online} where fromId='${userId}'`
        db(sql,(err,data)=>{
            if(err) reject(err);
        })

    },

}

